package com.company._2Field;

import com.company._1class.Person;

import java.lang.reflect.Field;

public class TestFiledDemo {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Class c= Person.class;

        //得到所有公开的字段
//        Field[] fs1=c.getFields();
//
//        for (Field f:fs1
//             ) {
//            System.out.println(f);
//        }

       Field[] fs2=c.getDeclaredFields();
                for (Field f:fs2
             ) {
            System.out.println(f);
        }

    Field f1=c.getField("nickname");
    Field f2=c.getDeclaredField("age");
    f2.setAccessible(true);

    Person p=new Person();
    f1.set(p,"老张");
    f2.set(p,20);
    }
}
